package com.zzw.spring.aop.advisor;

import com.zzw.spring.aop.vo.Waiter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.ClassFilter;
import org.springframework.aop.support.DynamicMethodMatcherPointcut;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zzw
 * @see
 * @since 2018/2/28
 */
//动态切面
public class GreetingDynamicPointcut extends DynamicMethodMatcherPointcut {

    private Logger logger = LoggerFactory.getLogger(getClass());

    private static List<String> specialClientList = new ArrayList<>();

    static {
        specialClientList.add("John");
        specialClientList.add("Tom");
    }

    @Override
    public ClassFilter getClassFilter() {
        return new ClassFilter() {
            @Override
            public boolean matches(Class<?> clazz) {
                return Waiter.class.isAssignableFrom(clazz);
            }
        };
    }

    @Override
    public boolean  matches(Method method, Class<?> clazz){
        logger.info("调用matches(method,clazz)" + clazz.getName() + "." + method.getName() + "做静态检查");
        return "greetTo".equals(method.getName());
    }

    @Override
    public boolean matches(Method method, Class<?> clazz, Object... args) {
        logger.info("调用matches(method,clazz)" + clazz.getName() + "." + method.getName() + "做动态检查");
        String name = (String) args[0];
        return specialClientList.contains(name);
    }
}
